package sjtu.ist.warehouse4ws.dao;

import java.util.List;

import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.orm.hibernate3.HibernateTemplate;

import sjtu.ist.warehouse4ws.model.RegistApplication;

/**
 * A data access object (DAO) providing persistence and search support for
 * RegistApplication entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see sjtu.ist.warehouse4ws.dao.RegistApplication
 * @author MyEclipse Persistence Tools
 */

public class RegistApplicationDao extends BaseDaoHibernate<RegistApplication> {
	private static final Logger log = LoggerFactory
			.getLogger(RegistApplicationDao.class);
	// property constants
	public static final String NAME = "name";
	public static final String PASSWORD = "password";
	public static final String COMPANY = "company";

	public Integer save(RegistApplication transientInstance) {
		log.debug("saving RegistApplication instance");
		Integer id=null;
		try {
			getHibernateTemplate().setFlushMode(HibernateTemplate.FLUSH_EAGER);   
			id=(Integer)getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
		return id;
	}

	public void delete(Integer id) {
		log.debug("deleting RegistApplication instance");
		try {
			getHibernateTemplate().setFlushMode(HibernateTemplate.FLUSH_EAGER);   
			getHibernateTemplate().delete(findById(id));
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public RegistApplication findById(java.lang.Integer id) {
		log.debug("getting RegistApplication instance with id: " + id);
		try {
			RegistApplication instance = (RegistApplication) getHibernateTemplate().get(
					"sjtu.ist.warehouse4ws.model.RegistApplication", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(RegistApplication instance) {
		log.debug("finding RegistApplication instance by example");
		try {
			List results = getHibernateTemplate()
					.findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding RegistApplication instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from RegistApplication as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List findByPassword(Object password) {
		return findByProperty(PASSWORD, password);
	}

	public List findByCompany(Object company) {
		return findByProperty(COMPANY, company);
	}

	public List findAll() {
		log.debug("finding all RegistApplication instances");
		try {
			String queryString = "from RegistApplication";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public RegistApplication merge(RegistApplication detachedInstance) {
		log.debug("merging RegistApplication instance");
		try {
			RegistApplication result = (RegistApplication) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(RegistApplication instance) {
		log.debug("attaching dirty RegistApplication instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(RegistApplication instance) {
		log.debug("attaching clean RegistApplication instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}